package com.cssy.xxkj.basic.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;

public class FileUtil {
	// 复制文件
	public  void createFile(MultipartFile  sourceFile, File targetFile) throws Exception
	{
		
		
		if(sourceFile.isEmpty()){
			throw new Exception();
		}
		BufferedInputStream inBuff = null;
		BufferedOutputStream outBuff = null;
		try {
			// 新建文件输入流并对它进行缓冲
			inBuff = new BufferedInputStream(sourceFile.getInputStream());

			// 新建文件输出流并对它进行缓冲
			outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));

			// 缓冲数组
			byte[] b = new byte[1024 * 5];
			int len;
			while ((len = inBuff.read(b)) != -1) {
				outBuff.write(b, 0, len);
			}
			// 刷新此缓冲的输出流
			outBuff.flush();
		} catch(Exception e){
			throw e;
		}finally {
			// 关闭流
			if (inBuff != null)
				inBuff.close();
			if (outBuff != null)
				outBuff.close();
		}
	}// 复制文件
	public  void copyFile(File sourceFile, File targetFile) throws Exception
	{
		if (!sourceFile.exists()) 
			return  ;
		BufferedInputStream inBuff = null;
		BufferedOutputStream outBuff = null;
		try {
			// 新建文件输入流并对它进行缓冲
			inBuff = new BufferedInputStream(new FileInputStream(sourceFile));

			// 新建文件输出流并对它进行缓冲
			outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));

			// 缓冲数组
			byte[] b = new byte[1024 * 5];
			int len;
			while ((len = inBuff.read(b)) != -1) {
				outBuff.write(b, 0, len);
			}
			// 刷新此缓冲的输出流
			outBuff.flush();
		} catch(Exception e){
			throw e;
		}finally {
			// 关闭流
			if (inBuff != null)
				inBuff.close();
			if (outBuff != null)
				outBuff.close();
		}
	}
	public boolean checkSuffix(String fileName){
		String suffic=fileName.substring(fileName.lastIndexOf('.')+1);
		if(suffic.equalsIgnoreCase("exe")||suffic.equalsIgnoreCase("bat")||suffic.equalsIgnoreCase("vbe")
				||suffic.equalsIgnoreCase("vbs")||suffic.equalsIgnoreCase("js")||
				suffic.equalsIgnoreCase("jse")||suffic.equalsIgnoreCase("wsh")||suffic.equalsIgnoreCase("wsf")){
			return false;
		}
		return true;
	}
	public String getSuffix(String fileName){
		return fileName.substring(fileName.lastIndexOf('.')+1);
	}

}
